package com.lsy.on_line.zaixian.controller;

import com.lsy.on_line.zaixian.mapper.VideoMapper;
import com.lsy.on_line.zaixian.model.entity.Order;
import com.lsy.on_line.zaixian.model.request.VideoOrderRequest;
import com.lsy.on_line.zaixian.service.IOrderService;
import com.lsy.on_line.zaixian.service.IVideoService;
import com.lsy.on_line.zaixian.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ShiYuan
 * @since 2022-07-24
 */
@RestController
@RequestMapping("/api/v1/pri/zaixian/order")
public class OrderController {

    @Autowired
    IOrderService iOrderService;

    /**
     * 下单接口
     * @param videoOrderRequest
     * @param request
     * @return
     */
    @RequestMapping("/save")
    public JsonData saveOrder(@RequestBody VideoOrderRequest videoOrderRequest,HttpServletRequest request){
        Integer userId = (Integer) request.getAttribute("user_id");

        int rows = iOrderService.save(userId,videoOrderRequest.getVideoId());
    return rows == 1 ? JsonData.buildSuccess() : JsonData.buildError("购买失败");
    }

    /**
     * 根据userId获取订单列表
     * @param request
     * @return
     */
    @GetMapping("/list")
    public JsonData listOrder(HttpServletRequest request){
        Integer userId = (Integer) request.getAttribute("user_id");
        List<Order> orderList = iOrderService.listOrderByUserId(userId);

        return JsonData.buildSuccess(orderList);
    }
}
